
$(function(){

	//省市选择
  $('.provinces li').bind('click',function(){
  	$(this).addClass('on').siblings('li').removeClass('on');
  })
  //全部省点击操作
  $('.all-pros').click(function(){

    if(!$('.citys').is(':animated')){
      $('.citys').slideToggle();
    }
  });

  //放大缩小还原操作
  $('.operator .big').click(function(){
  	$('#container').css('transform','scale(1.05)');
  });
  $('.operator .small').click(function(){
  	$('#container').css('transform','scale(0.8)');
  });
  $('.operator .normal').click(function(){
  	$('#container').css('transform','scale(1)');
  });

  //统计周期仿select下拉框
	$('.cycle .input').bind('click',function(e){
		if(!$('.cycle .selectDiv').is(":animated")){
	    $('.cycle .selectDiv').slideToggle(100);
	     e.stopPropagation();
		}
	})
	$('body').click(function(){
			$('.cycle .selectDiv').hide();
		})
	$('.cycle .selectDiv li').bind('click',function(){
		var html = $(this).html();
		$('.cycle .input').empty().text(html);
		$('.cycle .selectDiv').slideUp('fast');
	});

  //日期下拉仿select
  $('.lineChart .input').bind('click',function(e){
    if(!$('.lineChart .selectDiv').is(":animated")){
      $('.lineChart .selectDiv').slideToggle(100);
      e.stopPropagation();
    }
  })
  $('body').click(function(){
    $('.lineChart .selectDiv').hide();
  })
  $('.lineChart .selectDiv li').bind('click',function(){
    var html = $(this).html();
    $('.lineChart .input').empty().text(html);
    $('.lineChart .selectDiv').slideUp('fast');
  });

	//投资产品
  $('.products .kind span,.sexdiv span').click(function(){
  	$(this).toggleClass('on');
  });

  //防止点击过快背景变蓝
	$('.products .kind span,.sexdiv span').bind("selectstart", function () { return false; });

});




Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}






//日期选择默认值
function cycleSelect(val){
  var nowdate = new Date();
  var nowdate2 = new Date();
  nowdate.setDate(nowdate.getDate()-1);
  var date = nowdate.Format('yyyy-MM-dd');
  nowdate.setDate(nowdate.getDate()-6);
  var date2 = nowdate.Format('yyyy-MM-dd');
  nowdate2.setMonth(nowdate2.getMonth()-1);
  var date3 = nowdate2.Format('yyyy-MM-dd');
  nowdate2.setMonth(nowdate2.getMonth()-5);
  var date4 = nowdate2.Format('yyyy-MM-dd');
  var ms = nowdate2.getMonth()+1;
  var ms2 = nowdate.getMonth()+1;
  if(val == '小时'){
    $('#date-select').val(date2+' ~ '+date);
  }else if(val == '天'){
    $('#date-select').val(date3+' ~ '+date);
  }else if(val == '月'){
    if(ms>10){
      date4 = date4.slice(0,7);
    }else{
      date4 = date4.slice(0,7)
    }
    if(ms2>10){
      date = date.slice(0,7);
    }else{
      date = date.slice(0,7)
    }
    $('#date-select').val(date4+' ~ '+date);
    $('.apply-btn').bind('click',function(){
      var d1 = $('.normal-top .start-day').text().slice(0,7);
      var d2 = $('.normal-top .end-day').text().slice(0,7);
      $('#date-select').val(d1+' ~ '+d2);
    })
  }
}

//折线图、直方图调用
function lineChart(id,date,series,flag,type){
	var chart;
  chart = new Highcharts.Chart({
      chart: {
          renderTo: id,          //放置图表的容器
          plotBackgroundColor: null,
          plotBorderWidth: null,
          defaultSeriesType: type
      },
      xAxis: {//X轴数据
          categories: date,
          labels: {
              rotation: -45, //字体倾斜
              align: 'right',
              style: { font: 'normal 13px 宋体' }
          }
      },
      yAxis: {//Y轴显示文字
          title: {
              text: ''
          }
      },
      tooltip: {
          enabled: true,//鼠标上移的时候控制数据是否显示
          formatter: function() {
          		if(flag == 0){
          			return '<b style="font-weight:normal;">' + this.x + '</b><br/>' + this.series.name + ': ' + Highcharts.numberFormat(this.y, 1)+'人';
          		}else if(flag == 1){
          			return '<b style="font-weight:normal;">' + this.x + '</b><br/>' + this.series.name + ': ' + '￥' + Highcharts.numberFormat(this.y, 1);
          		}else if(flag == 2){
          			return '<b style="font-weight:normal;">' + this.x + '</b><br/>' + this.series.name + ': ' + Highcharts.numberFormat(this.y, 1)+'笔';
          		}

          }
      },
      plotOptions: {
          line: {
              dataLabels: {
                  enabled: false  //是否显示折线图数据
              },
              enableMouseTracking: true//是否显示title
          }
      },
      series: series
  });
  $('.highcharts-grid').hide();
}

//计算两个日期间的天数
function getDays(strDateStart,strDateEnd){
  var strSeparator = "-"; //日期分隔符
  var oDate1;
  var oDate2;
  var iDays;
  oDate1= strDateStart.split(strSeparator);
  oDate2= strDateEnd.split(strSeparator);
  var strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2]);
  var strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2]);
  iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24)//把相差的毫秒数转换为天数
  return iDays ;
}

//计算两个日期间的月数
function getMonths(date1 , date2){
  date1 = date1.split("-");
  date2 = date2.split("-");
  //获取年,月数
  var year1 = parseInt(date1[0]) ,
  month1 = parseInt(date1[1]) ,
  year2 = parseInt(date2[0]) ,
  month2 = parseInt(date2[1]) ,
  //通过年,月差计算月份差
  months = (year2 - year1) * 12 + (month2-month1) + 1;
  return months;
}

//计算两个日期间的所有年月份
function getYearAndMonth(start, end) {
  var result = [];
  var starts = start.split('-');
  var ends = end.split('-');
  var staYear = parseInt(starts[0]);
  var staMon = parseInt(starts[1])-1;
  var endYear = parseInt(ends[0]);
  var endMon = parseInt(ends[1]);
  while (staYear <= endYear) {
      if (staYear === endYear) {
          while (staMon < endMon) {
              staMon++;
              result.push({year: staYear, month: staMon});
          }
          staYear++;
      } else {
          staMon++;
          if (staMon > 12) {
              staMon = 1;
              staYear++;
          }
          result.push({year: staYear, month: staMon});
      }
  }
  return result;
}


//查询操作
function checkInfo(name,flag,products){
  var date = $('#date-select').val().toString();

  var startDate  = date.split('~')[0].toString();
  var endDate  = date.split('~')[1].toString();
  var max = parseInt($('.selected-days-num').text());
  var cycle = $('.cycle .input').text();
  var province = $('.provinces').find('li.on').text();
  var day = getDays(startDate,endDate)+1;
  if(products){
    if(!$('.kind span').hasClass('on')){
      alert('投资产品不能为空');
      $('.show-area').hide();
    }
  }
  if(province == '' || province == null){
    alert('省份不能为空');
  }else{
    if(cycle == '小时'){
      if(max>15){
        alert('统计周期为小时，日期最长不能超过15天');
        $('#hourBox').hide();
      }else{
        $('#hourBox').show().siblings('.show-area').hide();
        var lineObj = {
          series:[{
              name: name,
              data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5, 59.5, 14.5,28.4, 110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }],
          id:'container',
          date:['0-1点', '1-2点', '2-3点', '3-4点', '4-5点', '5-6点', '6-7点', '7-8点', '8-9点', '9-10点', '10-11点', '11-12点','12-13点','13-14点','14-15点','15-16点','16-17点','17-18点','18-19点','19-20点','20-21点','21-22点','22-23点','23-24点'],
          flag:flag,
          lineType:'line'
        }
        dateEach(day,endDate);
        lineChart(lineObj.id,lineObj.date,lineObj.series,lineObj.flag,lineObj.lineType);
      }
    }else if(cycle == '天'){
      if(max>30){
        alert('统计周期为天，日期最长不能超过1个月');
        $('#dayBox').hide();
      }else{
        $('#dayBox').show().siblings('.show-area').hide();
        var date = xaxisData(day,endDate);
        var lineObj = {
          series:[{
              name: '全福建',
              data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          },{
              name: '厦门',
              data:[137.0, 28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5, 59.5, 14.5,25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }],
          id:'container2',
          date:date,
          flag:flag,
          lineType:'line'
        }
        lineChart(lineObj.id,lineObj.date,lineObj.series,lineObj.flag,lineObj.lineType);
        if($('#container3').length>0){
          console.log(1);
        var lineObj2 = {
          series: [{
            name: 'PC注册',
            data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: '安卓注册',
            data:[93.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: 'IOS注册',
            data:[73.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: 'WAP注册',
            data:[53.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: 'IOS回馈版注册',
            data:[23.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: '未知来源注册',
            data:[33.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }],
          id:'container3',
          date:date,
          flag:flag,
          lineType:'line'
        }

        lineChart(lineObj2.id,lineObj2.date,lineObj2.series,lineObj2.flag,lineObj2.lineType);}
        if($('#container4').length>0){
        var lineObj3 = {
          series:[{
              name: '全福建',
              data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5]
          },{
              name: '厦门',
              data:[137.0, 28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5]
          },{
              name: '福州',
              data:[27.0, 328.4, 137.0, 36.9, 59.5, 14.5,28.4, 140.5]
          }],
          id:'container4',
          date:['0-20岁', '20-25岁', '25-30岁', '30-35岁', '35-40岁', '40-50岁', '50岁以上', '未知年龄'],
          flag:flag,
          lineType:'column'
        }
        lineChart(lineObj3.id,lineObj3.date,lineObj3.series,lineObj3.flag,lineObj3.lineType);}
        if($('#container5').length>0){
        var lineObj4 = {
          series: [{
            name: '67w',
            data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: '67m',
            data:[93.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: '77-97w',
            data:[73.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }, {
            name: '77-97m',
            data:[53.9, 12.2, 5.7, 18.5, 11.9, 15.2, 17.0, 16.6, 59.5, 14.5,28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5,110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
          }],
          id:'container5',
          date:date,
          flag:flag,
          lineType:'column'
        }
        lineChart(lineObj4.id,lineObj4.date,lineObj4.series,lineObj4.flag,lineObj4.lineType);}

      }
    }else if(cycle == '月'){
      if(max>180){
        alert('统计周期为月，日期最长不能超过6个月');
        $('#monthBox').hide();
      }else{
        $('#monthBox').show().siblings('.show-area').hide();
        day = getMonths(startDate,endDate);
        monthArr = getYearAndMonth(startDate,endDate);
        var lineObj5 = {
          series:[{
              name: '全福建',
              data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5,67]
          },{
              name: '厦门',
              data:[137.0, 28.4, 37.0, 36.9, 59.5, 14.5,45]
          }],
          id:'container6',
          date:[],
          flag:flag,
          lineType:'line'
        }
        for(var i=day-1;i>=0;i--){
          var s = monthArr[i].year+'年'+monthArr[i].month+'月';
          var html = '<li>'+s+'</li>'
          lineObj5.date.push(s);
        }
        var ar = lineObj5.date;
        ar.reverse();
        lineChart(lineObj5.id,lineObj5.date,lineObj5.series,lineObj5.flag,lineObj5.lineType);
        if($('#container7').length>0){
        var lineObj6 = {
          series: [{
            name: 'pc',
            data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5]
          }, {
            name: '安卓',
            data:[93.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }, {
            name: 'IOS',
            data:[73.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }, {
            name: 'WAP',
            data:[53.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }, {
            name: 'IOS回馈版',
            data:[23.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }, {
            name: '未知来源',
            data:[33.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }],
          id:'container7',
          date:date,
          flag:flag,
          lineType:'line'
        }
        lineChart(lineObj6.id,lineObj5.date,lineObj6.series,lineObj6.flag,lineObj6.lineType);}
        if($('#container8').length>0){
        var lineObj7 = {
          series:[{
              name: '全福建',
              data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5]
          },{
              name: '厦门',
              data:[137.0, 28.4, 37.0, 36.9, 59.5, 14.5,28.4, 140.5]
          },{
              name: '福州',
              data:[27.0, 328.4, 137.0, 36.9, 59.5, 14.5,28.4, 140.5]
          }],
          id:'container8',
          date:['0-20岁', '20-25岁', '25-30岁', '30-35岁', '35-40岁', '40-50岁', '50岁以上', '未知年龄'],
          flag:flag,
          lineType:'column'
        }
        lineChart(lineObj7.id,lineObj7.date,lineObj7.series,lineObj7.flag,lineObj7.lineType);}
        if($('#container9').length>0){
        var lineObj8 = {
          series: [{
            name: '67w',
            data:[37.0, 36.9, 59.5, 14.5,28.4, 110.5]
          }, {
            name: '67m',
            data:[93.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }, {
            name: '77-97w',
            data:[73.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }, {
            name: '77-97m',
            data:[53.9, 12.2, 5.7, 18.5, 11.9, 15.2]
          }],
          id:'container9',
          date:date,
          flag:flag,
          lineType:'column'
        }
        lineChart(lineObj8.id,lineObj5.date,lineObj8.series,lineObj8.flag,lineObj8.lineType);}
      }
    }
    $('.chart-title').find('.startDate').text(startDate);
    $('.chart-title').find('.endDate').text(endDate);
    var a = endDate.split('-')[1]+'月'+endDate.split('-')[2]+'日';
    $('.lineChart .selectDiv ul').empty();
    $('.lineChart .input').text(a);
    if(cycle == '小时'){
      day = day>7?7:day;
      dateEach(day,endDate);
    }
  }
}

//X轴坐标
function xaxisData(day,endDate){
  var xaxisData= [];
  for(var i=0;i<day;i++){
    var nowdate = new Date(endDate);
    nowdate.setDate(nowdate.getDate());
    var date = nowdate.toLocaleDateString();
    nowdate.setDate(nowdate.getDate()-i);
    var date2 = nowdate.toLocaleDateString();
    var date2 = date2.split('-')[1]+'月'+date2.split('-')[2]+'日';
    xaxisData.push(date2);
  }
  var reverse = xaxisData.reverse();
  return reverse;
}

//遍历日期下拉框
function dateEach(day,endDate,cycle){

  for(var i=0;i<day;i++){
    var nowdate = new Date(endDate);
    nowdate.setDate(nowdate.getDate());
    var date = nowdate.toLocaleDateString();
    nowdate.setDate(nowdate.getDate()-i);
    var date2 = nowdate.toLocaleDateString();
    var date2 = date2.split('-')[1]+'月'+date2.split('-')[2]+'日';
    var html = '<li>'+date2+'</li>'
    $('.lineChart .selectDiv ul').append(html);
    //lineObj.date.push(date2);
  }

  /*var ar2 = lineObj.date;
  ar2.reverse();*/
  selectClick(data);

  $('.lineChart .selectDiv li').bind('click',function(){
    var html = $(this).html();
    $('.lineChart .input').empty().text(html);
    $('.lineChart .selectDiv').slideUp('fast');
    var lineObj = {
      series:[{
          name: '注册人数',
          data:[137.0, 36.9, 59.5, 14.5,28.4, 110.5, 25.2, 26.5, 59.5, 14.5,28.4, 110.5, 25.2, 26.5,23.3, 18.3, 13.9, 9.6,4.5,28.4, 110.5, 25.2, 66.5,90]
      }],
      id:'container',
      date:['0-1点', '1-2点', '2-3点', '3-4点', '4-5点', '5-6点', '6-7点', '7-8点', '8-9点', '9-10点', '10-11点', '11-12点','12-13点','13-14点','14-15点','15-16点','16-17点','17-18点','18-19点','19-20点','20-21点','21-22点','22-23点','23-24点'],
      flag:0
    }

    lineChart(lineObj.id,lineObj.date,lineObj.series,lineObj.flag);
  });
}

/**
 * 创建表格
 * @param {字符串} theadStr 表头
 * @param {字符串} tbodyArr 主体
 * return 整个表格
 */
function createTable( theadStr, tbodyArr) {
  var _table;
  _table =`<table class="table" cellpadding="0" cellspacing="0" border="1" border-spacing='0' bordercolor="#000"><thead>`

  _table += theadStr
  _table +=`</thead><tbody>`
  tbodyArr.forEach(function(ele,index){
    _table += tbodyArr

  })

  _table +=`</tbody></table>`

  return _table
}


/**
 * 格式化金额
 * @param {String} num
 * @param {Boolean} bool 是否要小数
 */
function formatCurrency(num, bool) {
  if(num){
    bool = bool || false

    //将num中的$,去掉，将num变成一个纯粹的数据格式字符串
    num = num.toString().replace(/\$|\,/g,'');

    //如果num是负数，则获取她的符号
    var sign = num.indexOf("-")> -1 ? '-' : '';
    num = num.indexOf("-")> -1 ? num.substr(num.indexOf("-")+1) : num;

    //如果存在小数点，则获取数字的小数部分
    var cents = num.indexOf(".")> 0 ? num.substr(num.indexOf(".")) : '';
    cents = cents.length>1 ? cents : '' ;//注意：这里如果是使用change方法不断的调用，小数是输入不了的
    //获取数字的整数数部分
    num = num.indexOf(".")>0 ? num.substring(0,(num.indexOf("."))) : num ;

    //如果没有小数点，整数部分不能以0开头
    if('' == cents){
      if(num.length>1 && '0' == num.substr(0,1)){
        return '不是数字 ！ ';
      }
    }else{ //如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
      if(num.length>1 && '0' == num.substr(0,1)){
        return '不是数字 ！ ';
      }
    }
    // 增加 ,
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
      num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    }

    if(bool){
      return (sign + num + cents);
    }else {
      return (sign + num);
    }

    // 如果num不是数字，则将num置0，并返回
  }else{
    return 0;
  }
}


/**
 * 格式化时间
 * @param {String} format 时间格式 默认输出 2016/08/9
 */
Date.prototype.format = function(format) {
  format = format || 'yyyy/MM/d'
  var date = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "h+": this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "q+": Math.floor((this.getMonth() + 3) / 3),
    "S+": this.getMilliseconds()
  };
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (var k in date) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1
        ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    }
  }
  return format;
}
